package com.zhiyou.app.shop.manager.action;

import java.io.File;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.sql.JoinType;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.gson.JsonObject;
import com.zhiyou.app.shop.common.Authority;
import com.zhiyou.app.shop.common.AycshopException;
import com.zhiyou.app.shop.common.BaseAction;
import com.zhiyou.app.shop.common.InnerObject;
import com.zhiyou.app.shop.common.ShopManageConstants;
import com.zhiyou.app.shop.common.util.FileUtil;
import com.zhiyou.app.shop.common.util.Page;
import com.zhiyou.app.shop.manager.dao.pojo.Comment;
import com.zhiyou.app.shop.manager.dao.pojo.Goods;
import com.zhiyou.app.shop.manager.dao.pojo.GoodsCategory;
import com.zhiyou.app.shop.manager.dao.pojo.GoodsImg;
import com.zhiyou.app.shop.manager.dao.pojo.Shop;
import com.zhiyou.app.shop.manager.service.IGoodsManageService;
import com.zhiyou.app.shop.manager.service.IShopManageService;
import com.zhiyou.app.shop.manager.service.ISysManageService;

public class GoodsAction extends BaseAction {
	private static final long serialVersionUID = 1L;

	@Autowired
	private IGoodsManageService goodsService;
	@Autowired
	private IShopManageService shopService;
	@Autowired
	private ISysManageService sysManageService;
	
	private List<Goods> myGoods;
	private Goods goods = new Goods();
	private String ids;
	private List<GoodsCategory> categoryList;
	private Comment comment = new Comment();
	//-----------上传图片使用
	private File myGoodsImg;
	private String myGoodsImgFileName;
	private String myGoodsImgContentType;
	private String goodsAllImgPath="";
	//---上架与下架
	private boolean onSale;
	
	public GoodsAction() {
		super();
		this.setCurrentPath(getText("shopManage.path.myshop.goods"));
	}
	@Authority(2)
	public String myGoodsList(){
		Page page = getInitedPage();
		try {
			Shop myShop = shopService.findMyShop(this.getCurrentUser().getId());
			if(null != myShop){
				this.getGoods().setShop(myShop);
				goodsService.queryGoodsWithPage(page, goods, Order.desc("createDate"));
			}
		} catch (Exception e) {
			this.setErrorMessage(e);
		}
		
		return SUCCESS;
	}
	@Authority(2)
	public String addMyGoods(){
		try {
			Shop myShop = shopService.findMyShop(this.getCurrentUser().getId());
			goods = new Goods();
			goods.setShop(myShop);
			categoryList = sysManageService.getAllGoodsCats();
		} catch (Exception e) {
			this.setErrorMessage(e);
		}
		return SUCCESS;
	}

	/**加载上传图片iframe**/
	@Authority(2)
	public String goodsImgContainer(){
		if(StringUtils.isNotBlank(goods.getId()) && StringUtils.isBlank(goodsAllImgPath)){//说明是修改商品信息
			List<GoodsImg> imgList = goodsService.findGoodsImgByGoodsId(goods.getId());
			if(! imgList.isEmpty()){
				for(GoodsImg img : imgList){
					goodsAllImgPath += "," + img.getImgPath();
				}
				if(goodsAllImgPath.startsWith(",")){
					goodsAllImgPath = goodsAllImgPath.replaceFirst(",","");
				}
			}
		}
		return SUCCESS;
	}
	
	/**上传商品图片**/
	@Authority(2)
	public String uploadGoodsImg(){
		try {
			//一个产品可以上传的图片数不能超过规定值
			if(StringUtils.isNotBlank(goodsAllImgPath) && 
					goodsAllImgPath.split(",").length==ShopManageConstants.IMGSPERGOODS){
				
				String[] params = {String.valueOf(ShopManageConstants.IMGSPERGOODS)};
				
				throw new AycshopException("shopManage.goodsImg.max",params);
				
			}
			
			String goodsImgForder = ShopManageConstants.GOODSIMGFORDER+"/" + this.getCurrentUser().getId();
			
			String imgPath = FileUtil.aycShopUpload(myGoodsImg,myGoodsImgFileName, goodsImgForder);
			
			goodsAllImgPath +=  ","+ imgPath;
			
			if(goodsAllImgPath.startsWith(",")){
				
				goodsAllImgPath = goodsAllImgPath.replaceFirst(",","");
				
			}
		} catch (Exception e) {
			this.setErrorMessage(e);
		}
		return SUCCESS;
	}
	@Authority(2)
	public String editMyGoods(){
		try {
			goods = goodsService.findGoodsById(goods.getId());
			categoryList = sysManageService.getAllGoodsCats();
		} catch (Exception e) {
			this.setErrorMessage(e);
		}
		return SUCCESS;
	}
	@Authority(2)
	public String saveOrUpdateMyGoods(){
		try {
			Validator.validateGoodsAdd(goods);
			goodsService.saveOrUpdateGoods(goods);
		} catch (Exception e) {
			this.setErrorMessage(e);
			categoryList = sysManageService.getAllGoodsCats();
			return ERROR;
		}
		return SUCCESS;
	}
	@Authority(2)
	public String setSale(){
		this.printJSON(getResponse(), new InnerObject(){
			public void execute(JsonObject json) {
				goodsService.setSaleState(ids,onSale);
			}
		});
		return null;
	}
	@Authority(2)
	public String deleteMyGoods(){
		this.printJSON(getResponse(), new InnerObject(){
			public void execute(JsonObject json) {
				goodsService.deleteGoods(ids);
			}
		});
		return null;
	}
	
	/**
	 * 公司管理员查询所有商品
	 * @return
	 */
	@Authority(1)
	public String allGoodsList(){
		try {
			goods.setQueryShopInfo(Boolean.TRUE);//前台需要显示商铺信息
			goodsService.queryGoodsWithPage(getInitedPage(), goods, Order.desc("createDate"));
		} catch (Exception e) {
			this.setErrorMessage(e);
		}
		
		return SUCCESS;
	}
	/**
	 * 查看商品信息
	 * @return
	 */
	@Authority(2)
	public String goodsView(){
		try {
			goods = goodsService.findGoodsWithAssociate(goods.getId());
		} catch (Exception e) {
			this.setErrorMessage(e);
		}
		return SUCCESS;
	}
	
	/**
	 * 查看商品评论
	 * @return
	 */
	@Authority(2)
	public String goodsCommentList(){
		try {
			goodsService.findCommentsByGoods(getInitedPage(), goods);
		} catch (Exception e) {
			this.setErrorMessage(e);
		}
		return SUCCESS;
	}
	
	/**
	 * 删除一条商品评论
	 * @return
	 */
	@Authority(1)
	public String deleteGoodsComment(){
		
		this.printJSON(getResponse(), new InnerObject(){

			public void execute(JsonObject json) {
				goodsService.deleteGoodsComments(comment.getId());
			}
			
		});
		
		return null;
	}
	
	public List<Goods> getMyGoods() {
		return myGoods;
	}

	public void setMyGoods(List<Goods> myGoods) {
		this.myGoods = myGoods;
	}

	public Goods getGoods() {
		return goods;
	}

	public void setGoods(Goods goods) {
		this.goods = goods;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public List<GoodsCategory> getCategoryList() {
		return categoryList;
	}

	public void setCategoryList(List<GoodsCategory> categoryList) {
		this.categoryList = categoryList;
	}

	//-----------上传图片使用
	public File getMyGoodsImg() {
		return myGoodsImg;
	}

	public void setMyGoodsImg(File myGoodsImg) {
		this.myGoodsImg = myGoodsImg;
	}

	public String getMyGoodsImgFileName() {
		return myGoodsImgFileName;
	}

	public void setMyGoodsImgFileName(String myGoodsImgFileName) {
		this.myGoodsImgFileName = myGoodsImgFileName;
	}

	public String getMyGoodsImgContentType() {
		return myGoodsImgContentType;
	}

	public void setMyGoodsImgContentType(String myGoodsImgContentType) {
		this.myGoodsImgContentType = myGoodsImgContentType;
	}

	public String getGoodsAllImgPath() {
		return goodsAllImgPath;
	}

	public void setGoodsAllImgPath(String goodsAllImgPath) {
		this.goodsAllImgPath = goodsAllImgPath;
	}

	public boolean isOnSale() {
		return onSale;
	}

	public void setOnSale(boolean onSale) {
		this.onSale = onSale;
	}

	public Comment getComment() {
		return comment;
	}

	public void setComment(Comment comment) {
		this.comment = comment;
	}
	
}
